package com.yoyo.yyadsdk.bean;

import com.yoyo.yyadsdk.bean.dao.MonitorTimeEntity;

import java.util.Random;

public class TimeHmBean {
    private int mHour;
    private int mMinute;

    public TimeHmBean(int hour, int minute) {
        mHour = hour;
        mMinute = minute;
    }

    public int getHour() {
        return mHour;
    }

    public int getMinute() {
        return mMinute;
    }

    @Override
    public String toString() {
        return "TimeHmBean = " + mHour + ":" + mMinute;
    }

    /**
     * 获取时间段内的下次定时任务时间
     */
    public static TimeHmBean getTimeInPeriod(MonitorTimeEntity mM) {
        if (mM.getStartHour() == mM.getEndHour() && mM.getStartMinute() == mM.getEndMinute()) {
            return new TimeHmBean(mM.getStartHour(), mM.getStartMinute());
        }
        int sh, eh, sm, em, timeCount;
        sh = mM.getStartHour();
        eh = mM.getEndHour();
        sm = mM.getStartMinute();
        em = mM.getEndMinute();
        if (sh > eh) {
            sh -= 24;
        }
        if (sh == eh) {
            timeCount = Math.abs(em - sm);
        } else if (eh - sh == 1) {
            timeCount = 60 - sm + em;
        } else {
            timeCount = 60 * (eh - sh - 1) + 60 - sm + em;
        }
        int et = sh * 60 + sm + new Random().nextInt(timeCount + 1);
        if (et < 0)
            et += 24 * 60;
        return new TimeHmBean(et / 60, et % 60);
    }
}